package com.brian.guava.demo;

import com.google.common.util.concurrent.RateLimiter;

/**
 * 限流：令牌桶算法限流
 参考文章：https://blog.csdn.net/sunnyyoona/article/details/51228456

 * Created by Brian on 2018/4/25 14:38
 */
public class RateLimiterTest {
    public static void main(String[] args) {
        testNoRateLimiter();
        testWithRateLimiter();
    }

    public static void testNoRateLimiter() {
        Long start = System.currentTimeMillis();
        for (int i = 0; i < 10; i++) {
            System.out.println("call execute.." + i);
        }
        Long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    public static void testWithRateLimiter() {
        Long start = System.currentTimeMillis();
        RateLimiter limiter = RateLimiter.create(5.0); // 每秒不超过5个任务被提交
        for (int i = 0; i < 10; i++) {
            limiter.acquire(); // 请求RateLimiter, 超过permits会被阻塞
            System.out.println("call execute.." + i);
        }
        Long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}
